import Vue from 'vue'
import VueRouter from 'vue-router'
//导入登录子组件
import Login from '../components/login.vue'
import home from '../components/home.vue'
import users from '../components/users/user.vue'
import welcome from '../components/welcome.vue'
import roles from '../components/power/roles.vue'
import rights from '../components/power/rights.vue'
import cates from '../components/goods/cates.vue'
import params from '../components/goods/params.vue'
import list from '../components/goods/list.vue'
import add from '../components/goods/add.vue'

Vue.use(VueRouter)

//配置路由
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: welcome },
      { path: '/users', component: users },
      { path: '/roles', component: roles },
      { path: '/rights', component: rights },
      { path: '/categories', component: cates },
      { path: '/params', component: params },
      { path: '/goods', component: list },
      { path: '/goods/add', component: add },
    ]
  },

]

const router = new VueRouter({
  routes
})

//导航守卫是路由管理器中的一个方法
//在路由管理器寻找下一个url找对应子组件之前运行
//挂载路由导航守卫, 判断浏览器即将访问的页面是否需要token值
router.beforeEach((to, from, next) => {
  //判断是否登录login页面
  if (to.path === '/login') return next()

  //获取本地的sessionstorage值 如果访问需要token的页面并且没有带有token值的话就
  //退回到登录页面
  if (window.sessionStorage.getItem('token') == null) return next('/login')

  //如果带有token值,就可以访问所有的页面
  next()
})




export default router
